package com.inkwell.internet.productregistration.registration.portlet;

import java.util.Collections;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.RenderRequest;

import com.inkwell.internet.productregistration.model.PRProduct;
import com.inkwell.internet.productregistration.model.PRRegistration;
import com.inkwell.internet.productregistration.model.PRUser;
import com.inkwell.internet.productregistration.model.impl.PRProductImpl;
import com.inkwell.internet.productregistration.model.impl.PRRegistrationImpl;
import com.inkwell.internet.productregistration.model.impl.PRUserImpl;
import com.inkwell.internet.productregistration.service.PRProductLocalServiceUtil;
import com.inkwell.internet.productregistration.service.PRRegistrationLocalServiceUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;

public class ActionUtil {

	public static List<PRProduct> getProducts(RenderRequest request) {
		ThemeDisplay themeDisplay = (ThemeDisplay) request
				.getAttribute(WebKeys.THEME_DISPLAY);

		long groupId = themeDisplay.getScopeGroupId();
		List<PRProduct> tempResults;

		try {
			tempResults = PRProductLocalServiceUtil.getAllProducts(groupId);
		} catch (SystemException e) {
			tempResults = Collections.EMPTY_LIST;
		}

		return tempResults;
	}

	public static List<PRRegistration> getRegistration(RenderRequest request) {
		ThemeDisplay themeDisplay = (ThemeDisplay) request
				.getAttribute(WebKeys.THEME_DISPLAY);

		long groupId = themeDisplay.getScopeGroupId();
		List<PRRegistration> tempResults;

		try {
			tempResults = PRRegistrationLocalServiceUtil
					.getAllRegistrations(groupId);
		} catch (SystemException e) {
			tempResults = Collections.EMPTY_LIST;
		}

		return tempResults;
	}

	public static PRUser prUserFromRequest(ActionRequest request) {
		ThemeDisplay themeDisplay = (ThemeDisplay) request
				.getAttribute(WebKeys.THEME_DISPLAY);

		PRUser prUser = new PRUserImpl();

		prUser.setAddress1(ParamUtil.getString(request, "address1"));
		prUser.setAddress2(ParamUtil.getString(request, "address2"));
		prUser.setCity(ParamUtil.getString(request, "city"));
		prUser.setCompanyId(themeDisplay.getCompanyId());
		prUser.setCountry(ParamUtil.getString(request, "country"));
		prUser.setEmail(ParamUtil.getString(request, "emailAddress"));
		prUser.setFirstName(ParamUtil.getString(request, "firstName"));
		prUser.setLastName(ParamUtil.getString(request, "lastName"));
		prUser.setGender(ParamUtil.getString(request, "gender"));
		prUser.setGroupId(themeDisplay.getScopeGroupId());
		prUser.setPhoneNumber(ParamUtil.getString(request, "phoneNumber"));
		prUser.setPostalCode(ParamUtil.getString(request, "postalCode"));
		prUser.setState(ParamUtil.getString(request, "state"));

		int birthDateMonth = ParamUtil.getInteger(request, "birthDateMonth");
		int birthDateDay = ParamUtil.getInteger(request, "birthDateDay");
		int birthDateYear = ParamUtil.getInteger(request, "birthDateYear");

		prUser.setBirthDate(PortalUtil.getDate(birthDateMonth, birthDateDay,
				birthDateYear));

		return prUser;
	}

	public static PRRegistration prRegistrationFromRequest(ActionRequest request) {
		ThemeDisplay themeDisplay = (ThemeDisplay) request
				.getAttribute(WebKeys.THEME_DISPLAY);
		PRRegistration registration = new PRRegistrationImpl();

		registration.setCompanyId(themeDisplay.getCompanyId());
		registration.setGroupId(themeDisplay.getScopeGroupId());

		int datePurchasedMonth = ParamUtil.getInteger(request,
				"datePurchasedMonth");
		int datePurchasedDay = ParamUtil
				.getInteger(request, "datePurchasedDay");
		int datePurchasedYear = ParamUtil.getInteger(request,
				"datePurchasedYear");

		registration.setDatePurchased(PortalUtil.getDate(datePurchasedMonth,
				datePurchasedDay, datePurchasedYear));

		registration.setHowHear(ParamUtil.getString(request, "howHear"));
		registration.setProductId(Long.parseLong(ParamUtil.getString(request,
				"productType")));
		registration.setPrUserId(ParamUtil.getLong(request, "regUserId"));
		registration.setSerialNumber(ParamUtil.getString(request,
				"productSerialNumber"));
		registration.setWherePurchased(ParamUtil.getString(request,
				"wherePurchase"));

		return registration;
	}

	public static PRProduct productFromRequest(ActionRequest request) {
		ThemeDisplay themeDisplay = (ThemeDisplay) request
				.getAttribute(WebKeys.THEME_DISPLAY);
		PRProduct product = new PRProductImpl();

		product.setGroupId(themeDisplay.getScopeGroupId());
		product.setProductName(ParamUtil.getString(request, "productName"));
		product.setCompanyId(themeDisplay.getCompanyId());
		product.setSerialNumber(ParamUtil.getString(request, "productSerial"));

		return product;
	}
}
